// 提交链接 https://leetcode.cn/problems/minimum-time-to-complete-trips/submissions/565405336/
// 2187. 完成旅途的最少时间
// 完成日期： 2024.9.17
// 二分搜索
class Solution {
public:
    long long minimumTime(vector<int>& time, int totalTrips) {
        sort(time.begin(), time.end());
        long long head = 1, tail = (long long)time.back() * totalTrips;
        while (head < tail)
        {
            long long mid = (head + tail) / 2;
            long long temp = totalTrips;
            for (int i = 0; i < time.size(); i++)
            {
                temp -= mid / (long long)time[i];
                if (temp <= 0) break;
            }
            if (temp <= 0) tail = mid;
            else head = mid + 1;
        }
        return head;
    }
};